﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGym.Models;
using System.Data.Linq;

namespace EGym.Controllers
{
    public class AlunoController : Controller
    {
        EGymModeloDataContext modelo = new EGymModeloDataContext();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            List<Aluno> listaAlunos = (from a in modelo.Alunos
                                       select a).ToList();
            
            return View(listaAlunos);
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Home/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Aluno novoAluno)
        {
            if (!ModelState.IsValid)
                return View();
            try
            {
                // TODO: Add insert logic here

                modelo.GetTable<Aluno>().InsertOnSubmit(novoAluno);
                modelo.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            var aluno = (from a in modelo.Alunos
                         where a.AlunoId == id
                         select a).FirstOrDefault();
            return View(aluno);
        }
        
        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(Aluno editaAluno)
        {
            if (!ModelState.IsValid)
                return View();
            try
            {
                // TODO: Add update logic here
                modelo.Alunos.Attach(editaAluno);
                modelo.Refresh(RefreshMode.KeepCurrentValues, editaAluno);
                modelo.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5
 
        public ActionResult Delete(int id)
        {
            //var aluno = (from a in modelo.Alunos
            //             where a.AlunoId == id
            //             select a).FirstOrDefault();
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Aluno deletaAluno)
        {
            if (!ModelState.IsValid)
                return View();
            try
            {
                // TODO: Add delete logic here
                deletaAluno = (from a in modelo.Alunos 
                               where a.AlunoId == id 
                               select a).FirstOrDefault();
                modelo.Alunos.DeleteOnSubmit(deletaAluno);
                modelo.SubmitChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
